From e11356ceed83425c19d51fdc17d28d3125037f20 Mon Sep 17 00:00:00 2001 From: robertl Date: Fri, 2 Dec 2005 19:14:38 +0000 Subject: [PATCH] Move color parser from an1 to util.c. Teach Ozi cmd line parser to use it. --- an1.c | 83 +-------------------------------------------- defs.h | 5 +++ ozi.c | 14 ++++++-- util.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 85 deletions(-) diff --git a/an1.c b/an1.c index 64cc40740..394922182 100644 --- a/an1.c +++ b/an1.c @@ -1042,87 +1042,6 @@ Init_Road_Changes( void ) xfree( copy ); } -int HexDigit( char hex ) { - const char *Digits = "0123456789ABCDEF"; - const char *digits = "0123456789abcdef"; - char * ofs = strchr( digits, hex ); - if ( ofs ) { - return ofs-digits; - } - - ofs = strchr( Digits, hex ); - if ( ofs ) { - return ofs-Digits; - } - return 0; -} - -int HexByte( char* hex ) { - int b = (HexDigit(hex[0])<<4)+HexDigit(hex[1]); - return b; -} - -void Init_Color( void ) { - if ( opt_color[0] == '#' ) { - opt_color_num = (HexByte( opt_color+1 )) + // red - (HexByte( opt_color+3 )<<8) + // green - (HexByte( opt_color+5 )<<16); // blue - } - else if ( !case_ignore_strcmp( opt_color, "aqua" ) || - !case_ignore_strcmp( opt_color, "cyan" )) { - opt_color_num = 0xffff00; - } - else if ( !case_ignore_strcmp( opt_color, "black" )) { - opt_color_num = 0x000000; - } - else if ( !case_ignore_strcmp( opt_color, "blue" )) { - opt_color_num = 0xff0000; - } - else if ( !case_ignore_strcmp( opt_color, "fuchsia" ) || - !case_ignore_strcmp( opt_color, "magenta" )) { - opt_color_num = 0xff00ff; - } - else if ( !case_ignore_strcmp( opt_color, "gray" )) { - opt_color_num = 0x808080; - } - else if ( !case_ignore_strcmp( opt_color, "green" )) { - opt_color_num = 0x008000; - } - else if ( !case_ignore_strcmp( opt_color, "lime" )) { - opt_color_num = 0x00ff00; - } - else if ( !case_ignore_strcmp( opt_color, "maroon" )) { - opt_color_num = 0x000080; - } - else if ( !case_ignore_strcmp( opt_color, "navy" )) { - opt_color_num = 0x800000; - } - else if ( !case_ignore_strcmp( opt_color, "olive" )) { - opt_color_num = 0x008080; - } - else if ( !case_ignore_strcmp( opt_color, "purple" )) { - opt_color_num = 0x800080; - } - else if ( !case_ignore_strcmp( opt_color, "red" )) { - opt_color_num = 0x0000ff; - } - else if ( !case_ignore_strcmp( opt_color, "silver" )) { - opt_color_num = 0xc0c0c0; - } - else if ( !case_ignore_strcmp( opt_color, "teal" )) { - opt_color_num = 0x808000; - } - else if ( !case_ignore_strcmp( opt_color, "white" )) { - opt_color_num = 0xffffff; - } - else if ( !case_ignore_strcmp( opt_color, "yellow" )) { - opt_color_num = 0x00ffff; - } - else { - fatal( MYNAME ": unrecognized color name\n" ); - } -} - static void rd_init(const char *fname) { @@ -1150,7 +1069,7 @@ wr_init(const char *fname) outfile = xfopen( fname, "wb", MYNAME ); Init_Output_Type(); Init_Road_Changes(); - Init_Color(); + opt_color_num = color_to_bbggrr(opt_color); Init_Wpt_Type(); if ( opt_zoom ) { opt_zoom_num = atoi(opt_zoom); diff --git a/defs.h b/defs.h index fb3e6c2cc..ea8a848cf 100644 --- a/defs.h +++ b/defs.h @@ -683,6 +683,11 @@ double degrees2ddmm(double deg_val); */ unsigned long get_crc32(const void * data, int datalen); +/* + * Color helpers. + */ +int color_to_bbggrr(char *cname); + /* * A constant for unknown altitude. It's tempting to just use zero * but that's not very nice for the folks near sea level. diff --git a/ozi.c b/ozi.c index af3b94a3d..8238678f2 100644 --- a/ozi.c +++ b/ozi.c @@ -50,6 +50,10 @@ static char *snlenopt = NULL; static char *snwhiteopt = NULL; static char *snupperopt = NULL; static char *snuniqueopt = NULL; +static char *wptfgcolor = NULL; +static char *wptbgcolor = NULL; + + static arglist_t ozi_args[] = { @@ -61,6 +65,10 @@ arglist_t ozi_args[] = { NULL, ARGTYPE_BOOL}, {"snunique", &snuniqueopt, "Make synth. shortnames unique", NULL, ARGTYPE_BOOL}, + {"wptfgcolor", &wptfgcolor, "Waypoint foreground color", + "black", ARGTYPE_INT}, + {"wptbgcolor", &wptbgcolor, "Waypoint background color", + "yellow", ARGTYPE_INT}, {0, 0, 0, 0, 0} }; @@ -92,9 +100,9 @@ ozi_alloc_fsdata(void) fsdata->fs.copy = (fs_copy) ozi_copy_fsdata; fsdata->fs.destroy = ozi_free_fsdata; - /* Provide reasonable defaults */ - fsdata->fgcolor = 0; - fsdata->bgcolor = 65535; + /* Provide defaults via command line defaults */ + fsdata->fgcolor = color_to_bbggrr(wptfgcolor); + fsdata->bgcolor = color_to_bbggrr(wptbgcolor); return fsdata; } diff --git a/util.c b/util.c index fa0438584..b3457cd57 100644 --- a/util.c +++ b/util.c @@ -1084,3 +1084,108 @@ char *xml_attribute( xml_tag *tag, char *attrname ) } return result; } + +/* + * Functions for converting human-readable colors to BBGGRR value. + * Substantial optimization opportunities remain. + */ +int HexDigit( char hex ) { + const char *Digits = "0123456789ABCDEF"; + const char *digits = "0123456789abcdef"; + char * ofs = strchr( digits, hex ); + if ( ofs ) { + return ofs-digits; + } + + ofs = strchr( Digits, hex ); + if ( ofs ) { + return ofs-Digits; + } + return 0; +} + +int HexByte( char* hex ) { + int b = (HexDigit(hex[0])<<4)+HexDigit(hex[1]); + return b; +} + +/* + * Given an input of the form: + * # + * + * + * return the BBGGRR value for it. + */ + +int +color_to_bbggrr( char *opt_color ) +{ + int color_num; + char *ep; + + color_num = strtol(opt_color, &ep, 10); + + if (ep != opt_color) { + return color_num; + } + else if ( opt_color[0] == '#' ) { + color_num = (HexByte( opt_color+1 )) + // red + (HexByte( opt_color+3 )<<8) + // green + (HexByte( opt_color+5 )<<16); // blue + } + else if ( !case_ignore_strcmp( opt_color, "aqua" ) || + !case_ignore_strcmp( opt_color, "cyan" )) { + color_num = 0xffff00; + } + else if ( !case_ignore_strcmp( opt_color, "black" )) { + color_num = 0x000000; + } + else if ( !case_ignore_strcmp( opt_color, "blue" )) { + color_num = 0xff0000; + } + else if ( !case_ignore_strcmp( opt_color, "fuchsia" ) || + !case_ignore_strcmp( opt_color, "magenta" )) { + color_num = 0xff00ff; + } + else if ( !case_ignore_strcmp( opt_color, "gray" )) { + color_num = 0x808080; + } + else if ( !case_ignore_strcmp( opt_color, "green" )) { + color_num = 0x008000; + } + else if ( !case_ignore_strcmp( opt_color, "lime" )) { + color_num = 0x00ff00; + } + else if ( !case_ignore_strcmp( opt_color, "maroon" )) { + color_num = 0x000080; + } + else if ( !case_ignore_strcmp( opt_color, "navy" )) { + color_num = 0x800000; + } + else if ( !case_ignore_strcmp( opt_color, "olive" )) { + color_num = 0x008080; + } + else if ( !case_ignore_strcmp( opt_color, "purple" )) { + color_num = 0x800080; + } + else if ( !case_ignore_strcmp( opt_color, "red" )) { + color_num = 0x0000ff; + } + else if ( !case_ignore_strcmp( opt_color, "silver" )) { + color_num = 0xc0c0c0; + } + else if ( !case_ignore_strcmp( opt_color, "teal" )) { + color_num = 0x808000; + } + else if ( !case_ignore_strcmp( opt_color, "white" )) { + color_num = 0xffffff; + } + else if ( !case_ignore_strcmp( opt_color, "yellow" )) { + color_num = 0x00ffff; + } + else { + fatal( "unrecognized color name %s\n", opt_color ); + } + + return color_num; +} -- 2.30.2